﻿var calDataByBranch;
var alreadyCal;

jQuery(document).ready(function () {
    getImported();
    getLocation();
    jQuery('#LoadingImage').dialog({
        title: 'Uploading!',
        width: 235,
        height: 237,
        closed: true,
        cache: false,
        modal: true,

    });
});

function showLoading() {
    $('#LoadingImage').dialog('open');
}

function stopLoading() {
    $('#LoadingImage').dialog('close');
}

function getCalPi() {
    var branchSelect = jQuery("#branchAll").datagrid("getSelections");
    var getCalByBranch = [];
    jQuery.each(branchSelect, function (i, d) {
        getCalByBranch.push(jQuery.trim(d.mprovtrans));
    });
    showLoading();
    jQuery.ajax({
        type: "Post",
        url: "/api/ProductApi/PostCalSellout",
        data: JSON.stringify(getCalByBranch),
        contentType: "application/json",
        success: function (result) {
            calDataByBranch = result;
            alreadyCal = getCalByBranch;
            getLocation();
            stopLoading();
            alert("Calculate Finish");
        },
        error: function (data) {
            stopLoading();
            if (func) {
                func(data);
            }
        }
    });

}

function confirmDraft(brancCode) {
    jQuery.ajax({
        type: "Get",
        url: "/api/ProductApi/GetConfirmDraftPi",
        data: { "branchCode": brancCode },
        contentType: "application/json",
        success: function (result) {
            if (result == "true" || result == true) {
                alert("Create Draft Success");
            }
            else {
                alert("Cannot create draft");
            }
        },
        error: function (data) {
            if (func) {
                func(data);
            }
        }
    });
}

function formatCreateDraft(val, data, index) {
    if (data.mprovtrans === null || data.mprovtrans === '') {
        return "";
    }
    else {
        if (alreadyCal != undefined || alreadyCal != null) {
            var productCode = jQuery.trim(data.mprovtrans);
            if (jQuery.inArray(productCode, alreadyCal) !== -1)
            {
                return '<a href="#" onclick=javascript:confirmDraft("' + productCode + '");>Create</a>';
            }
            return "";
        }
        else {
            return "";
        }
    }
}


function getImported() {
    jQuery.ajax({
        url: "/api/ProductApi/GetImport",
        context: document.body
    }).done(function(data) {
        var d = data;
        jQuery("#stockPath").html(data.StockPath)
        jQuery("#sellOutPath").html(data.SellOutPath)
    });
    
}



function getLocation(func) {
    jQuery.ajax({
        type: "GET",
        url: "/api/BranchApi/GetAllActiveBranch",
        success: function (result) {
            jQuery('#branchAll').datagrid({
                data: result.AllBranch
            });
        },
        error: function (data) {
            if (func) {
                func(data);
            }
        }

    });
}